import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { CreateReportManagementDto } from './dto/create-report-management.dto';
import { UpdateReportManagementDto } from './dto/update-report-management.dto';
import { Repository, Like, Between, Not } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { ReportManagement } from './entities/report-management.entity'

@Injectable()
export class ReportManagementService {

    constructor(@InjectRepository(ReportManagement) private readonly reportManagement: Repository<ReportManagement>) { }
    async create(createReportManagementDto: CreateReportManagementDto) {
        // 查询数据库中是否已存在该 reportDate,type 的记录
        const existingRecord = await this.reportManagement.findOneBy({ reportDate: createReportManagementDto.reportDate, type: createReportManagementDto.type });
        // reportDate已存在不允许再添加
        if (existingRecord) {
            const errorMessage = existingRecord.type === 'day' ? '该日期的日报已存在' : '该日期的月报已存在'
            throw new HttpException(errorMessage, HttpStatus.BAD_REQUEST);
        }
        const data = new ReportManagement()
        data.type = createReportManagementDto.type
        data.reportDate = createReportManagementDto.reportDate
        data.data = createReportManagementDto.data
        return this.reportManagement.save(data)
    }

    // 分页查询
    async findAllByPage(query: { type: string, dateFrom: string, dateTo: string, keyword: string, pageNo: number, pageSize: number }) {
        const where = {
            data: Like(`%${query.keyword}%`),
            reportDate: null,
            type: query.type || null
        }
        if (query.dateFrom && query.dateTo) {
            where.reportDate = Between(query.dateFrom, query.dateTo)
        }
        const records = await this.reportManagement.find({
            where,
            order: {
                reportDate: "DESC"
            },
            skip: (query.pageNo - 1) * query.pageSize,
            take: query.pageSize,
        })
        const total = await this.reportManagement.count({
            where,
        })
        return {
            records,
            total
        }
    }

    // 不分页查询
    async findAll({ dateFrom = '', dateTo = '', keyword = '' }: { dateFrom: string, dateTo: string, keyword: string }) {
        let where = null
        if (dateFrom && dateTo) {
            where = {
                data: Like(`%${keyword || ''}%`),
                reportDate: Between(dateFrom, dateTo)
            }
        } else {
            where = {
                data: Like(`%${keyword || ''}%`),
            }
        }
        const records = await this.reportManagement.find({
            where,
            order: {
                reportDate: "DESC"
            },
        })
        return records
    }

    findOne(id: any) {
        return this.reportManagement.findOneBy({ id })
    }

    async update(id: number, updateReportManagementDto: UpdateReportManagementDto) {
        // 查询数据库中是否已存在该 reportDate,type 的记录
        const existingRecord = await this.reportManagement.findOneBy({
            reportDate: updateReportManagementDto.reportDate,
            type: updateReportManagementDto.type,
            id: Not(id)
        });
        // reportDate已存在不允许再添加
        if (existingRecord) {
            const errorMessage = existingRecord.type === 'day' ? '该日期的日报已存在' : '该日期的月报已存在'
            throw new HttpException(errorMessage, HttpStatus.BAD_REQUEST);
        }

        return this.reportManagement.update(id, updateReportManagementDto)
    }

    remove(id: number) {
        return this.reportManagement.delete(id)
    }
}
